﻿using System.Linq;
using MS.InformationManagement.BLL.DeliveryMeans;
using MS.InformationManagement.Presenter.Support;

namespace MS.InformationManagement.Presenter.DeliveryMeans
{
    public class DeliveryMeanConfigPresenter : BasePresenter<IDeliveryMeanConfigView>
    {
        public DeliveryMeanConfigPresenter(IDeliveryMeanConfigView view) : base(view) { }

        public void AddDeliveryMean()
        {
            DeliveryMeansConfigBLL.AddDeliveryMean(View.DeliveryMeanTitle, View.IsSecureDeliveryMean, View.DeliveryMeanConfigItems);
        }

        public bool CanAddDeliveryMean()
        {
            return !DeliveryMeansConfigBLL.DeliveryMeanExists(View.DeliveryMeanTitle, View.IsSecureDeliveryMean);
        }

        public bool CanUpdateDeliveryMean()
        {
            return !DeliveryMeansConfigBLL.DeliveryMeanExistsForUpdate(View.DeliveryMeanId, View.DeliveryMeanTitle, View.IsSecureDeliveryMean);
        }

        public void UpdateDeliveryMean()
        {
            DeliveryMeansConfigBLL.UpdateDeliveryMean(View.DeliveryMeanId, View.DeliveryMeanTitle, View.DeliveryMeanConfigItems);
        }

        public void LoadDeliveryMean()
        {
            System.Collections.Generic.List<DAL.DocumentCenter.ConfiguracionDeCostosDeMedioDeEnvio> entities = DeliveryMeansConfigBLL.GetDeliveryMean(View.SelectedConfigId).ToList();
            View.DeliveryMeanId = entities[0].MedioEnvioCostos.Id.Value;
            View.DeliveryMeanTitle = entities[0].MedioEnvioCostos.Title;
            System.Collections.Generic.List<DAL.DTOs.DeliveryMeanConfigDTO> deliveryMeanConfigItems = new System.Collections.Generic.List<DAL.DTOs.DeliveryMeanConfigDTO>();

            entities.ForEach(delegate(DAL.DocumentCenter.ConfiguracionDeCostosDeMedioDeEnvio currentItem)
            {
                deliveryMeanConfigItems.Add(new DAL.DTOs.DeliveryMeanConfigDTO
                {
                    Id = currentItem.Id.Value,
                    LocalCost = currentItem.CostoLocal.Value,
                    LowerWeightLimit = currentItem.LímiteInferior.Value,
                    UpperWeightLimit = currentItem.LímiteSuperior.Value,
                    NationalCost = currentItem.CostoNacional.Value
                });
            });

            View.DeliveryMeanConfigItems = deliveryMeanConfigItems;
        }

        public void LoadDeliveryMeans()
        {
            int totalRecords;
            View.DeliveryMeans = DeliveryMeansConfigBLL.GetDeliveryMeans(View.PageIndex, View.PageSize, out totalRecords).ToList();
            View.TotalRecords = totalRecords;
        }

        public void HandleLoad()
        {
            View.DeliveryMeanTitle = string.Empty;
            View.DeliveryMeanConfigItems = new System.Collections.Generic.List<DAL.DTOs.DeliveryMeanConfigDTO>(new DAL.DTOs.DeliveryMeanConfigDTO[] { new DAL.DTOs.DeliveryMeanConfigDTO { } });
            View.PageIndex = 0;
            LoadDeliveryMeans();
        }

        public void DeleteDeliveryMean()
        {
            DeliveryMeansConfigBLL.DeleteDeliveryMean(View.SelectedConfigId);
        }

        public bool AreDeliveryConfigItemsValid()
        {
            bool areValid = true;
            System.Collections.Generic.List<DAL.DTOs.DeliveryMeanConfigDTO> deliveryMeanConfigItems = View.DeliveryMeanConfigItems;
            foreach (DAL.DTOs.DeliveryMeanConfigDTO currentItem in deliveryMeanConfigItems)
            {
                areValid = areValid && !(deliveryMeanConfigItems
                    .Except(new DAL.DTOs.DeliveryMeanConfigDTO[] { currentItem })
                    .Count(dmcDTO => currentItem.LowerWeightLimit >= dmcDTO.LowerWeightLimit
                        && currentItem.UpperWeightLimit <= dmcDTO.UpperWeightLimit) > 0);
            }
            return areValid;
        }
    }
}
